package com.ruoyi.campus.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.campus.domain.UserExtend;
import com.ruoyi.campus.service.IUserExtendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户扩展信息Controller
 * 
 * @author ruoyi
 * @date 2024-01-17
 */
@RestController
@RequestMapping("/campus/userExtend")
public class UserExtendController extends BaseController
{
    @Autowired
    private IUserExtendService userExtendService;

    /**
     * 查询用户扩展信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserExtend userExtend)
    {
        startPage();
        List<UserExtend> list = userExtendService.selectUserExtendList(userExtend);
        return getDataTable(list);
    }

    /**
     * 导出用户扩展信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:user:export')")
    @Log(title = "用户扩展信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserExtend userExtend)
    {
        List<UserExtend> list = userExtendService.selectUserExtendList(userExtend);
        ExcelUtil<UserExtend> util = new ExcelUtil<UserExtend>(UserExtend.class);
        util.exportExcel(response, list, "用户扩展信息数据");
    }

    /**
     * 查询用户类型统计
     */
    @PreAuthorize("@ss.hasPermi('campus:user:statistics')")
    @GetMapping("/statistics")
    public AjaxResult getUserTypeStatistics(@RequestParam(required = false) Long schoolId)
    {
        List<Map<String, Object>> statistics = userExtendService.selectUserTypeStatistics(schoolId);
        return success(statistics);
    }

    /**
     * 查询用户类型统计
     */
    @PreAuthorize("@ss.hasPermi('campus:user:statistics')")
    @GetMapping("/statistics/type")
    public AjaxResult getUserTypeStatisticsDetailed(@RequestParam(required = false) Long schoolId)
    {
        List<Map<String, Object>> statistics = userExtendService.selectUserTypeStatistics(schoolId);
        return success(statistics);
    }

    /**
     * 查询用户年龄分布统计
     */
    @PreAuthorize("@ss.hasPermi('campus:user:statistics')")
    @GetMapping("/statistics/age")
    public AjaxResult getAgeStatistics(@RequestParam(required = false) Long schoolId)
    {
        List<Map<String, Object>> statistics = userExtendService.selectAgeStatistics(schoolId);
        return success(statistics);
    }

    /**
     * 查询班级统计
     */
    @PreAuthorize("@ss.hasPermi('campus:user:statistics')")
    @GetMapping("/statistics/class")
    public AjaxResult getClassStatistics(@RequestParam(required = false) Long schoolId)
    {
        List<Map<String, Object>> statistics = userExtendService.selectClassStatistics(schoolId);
        return success(statistics);
    }

    /**
     * 获取用户扩展信息详细信息（包含关联信息）
     */
    @PreAuthorize("@ss.hasPermi('campus:user:query')")
    @GetMapping(value = "/detail/{userId}")
    public AjaxResult getDetail(@PathVariable("userId") Long userId)
    {
        return success(userExtendService.selectUserExtendDetail(userId));
    }

    /**
     * 获取用户扩展信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(userExtendService.selectUserExtendByUserId(userId));
    }

    /**
     * 根据学号查询学生
     */
    @PreAuthorize("@ss.hasPermi('campus:user:query')")
    @GetMapping("/student")
    public AjaxResult getStudentByNumber(@RequestParam(required = false) Long schoolId, 
                                        @RequestParam String studentNumber)
    {
        return success(userExtendService.selectStudentByNumber(schoolId, studentNumber));
    }

    /**
     * 根据工号查询教师
     */
    @PreAuthorize("@ss.hasPermi('campus:user:query')")
    @GetMapping("/teacher")
    public AjaxResult getTeacherByNumber(@RequestParam(required = false) Long schoolId, 
                                        @RequestParam String teacherNumber)
    {
        return success(userExtendService.selectTeacherByNumber(schoolId, teacherNumber));
    }

    /**
     * 查询学校的用户列表
     */
    @PreAuthorize("@ss.hasPermi('campus:user:list')")
    @GetMapping("/school")
    public AjaxResult getUsersBySchool(@RequestParam(required = false) Long schoolId, 
                                      @RequestParam(required = false) Integer userType)
    {
        List<UserExtend> list = userExtendService.selectUsersBySchool(schoolId, userType);
        return success(list);
    }

    /**
     * 查询学生列表
     */
    @PreAuthorize("@ss.hasPermi('campus:user:list')")
    @GetMapping("/students")
    public AjaxResult getStudents(@RequestParam(required = false) Long schoolId, 
                                 @RequestParam(required = false) String className,
                                 @RequestParam(required = false) String grade)
    {
        List<UserExtend> list = userExtendService.selectStudents(schoolId, className, grade);
        return success(list);
    }

    /**
     * 查询教师列表
     */
    @PreAuthorize("@ss.hasPermi('campus:user:list')")
    @GetMapping("/teachers")
    public AjaxResult getTeachers(@RequestParam(required = false) Long schoolId, 
                                 @RequestParam(required = false) String department,
                                 @RequestParam(required = false) String subject)
    {
        List<UserExtend> list = userExtendService.selectTeachers(schoolId, department, subject);
        return success(list);
    }

    /**
     * 查询家长列表
     */
    @PreAuthorize("@ss.hasPermi('campus:user:list')")
    @GetMapping("/parents")
    public AjaxResult getParents(@RequestParam(required = false) Long schoolId)
    {
        List<UserExtend> list = userExtendService.selectParents(schoolId);
        return success(list);
    }

    /**
     * 新增用户扩展信息
     */
    @PreAuthorize("@ss.hasPermi('campus:user:add')")
    @Log(title = "用户扩展信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserExtend userExtend)
    {
        return toAjax(userExtendService.insertUserExtend(userExtend));
    }

    /**
     * 修改用户扩展信息
     */
    @PreAuthorize("@ss.hasPermi('campus:user:edit')")
    @Log(title = "用户扩展信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserExtend userExtend)
    {
        return toAjax(userExtendService.updateUserExtend(userExtend));
    }

    /**
     * 删除用户扩展信息
     */
    @PreAuthorize("@ss.hasPermi('campus:user:remove')")
    @Log(title = "用户扩展信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(userExtendService.deleteUserExtendByUserIds(userIds));
    }

    /**
     * 批量导入用户
     */
    @PreAuthorize("@ss.hasPermi('campus:user:import')")
    @Log(title = "用户扩展信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestBody List<UserExtend> userList, 
                                @RequestParam(required = false, defaultValue = "false") Boolean updateSupport)
    {
        return success(userExtendService.importUser(userList, updateSupport));
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<UserExtend> util = new ExcelUtil<UserExtend>(UserExtend.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 校验学号/工号唯一性
     */
    @PostMapping("/checkNumberUnique")
    public AjaxResult checkNumberUnique(@RequestBody UserExtend userExtend)
    {
        return success(userExtendService.checkNumberUnique(userExtend));
    }

    /**
     * 重置用户密码
     */
    @PreAuthorize("@ss.hasPermi('campus:user:resetpwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody UserExtend userExtend)
    {
        // TODO: 需要根据实际UserExtend类结构调整密码字段获取方式
        return toAjax(userExtendService.resetUserPwd(userExtend.getUserId(), "123456")); // 临时使用默认密码
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('campus:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody UserExtend userExtend)
    {
        return toAjax(userExtendService.updateUserStatus(userExtend));
    }
} 